package com.enqualcomm.kids.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.bean.RelationShip;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.manager.Controller;

import java.io.IOException;
import java.util.Locale;

/**
 * Created by yinzhiqun on 2015/6/16.
 * 设置家长与手表关系gridview的适配器
 */
public class SetRelationAdapter extends BaseAdapter {
    private Context context;
    private final LayoutInflater inflater;
    private int selectedPosition = 0;

    public SetRelationAdapter(Context context){
        this.context = context;
        inflater = LayoutInflater.from(context);
        initData();
    }

    private void initData() {
        Controller controller = Controller.getInstance();
            if (/*new AppDefault().isOldManMode()*/controller.isOldMainMode()){//老人版本
                res = new RelationShip[]{
//                        new RelationShip("儿子", "relationship_1.png"),
//                        new RelationShip("儿媳", "relationship_2.png"),
//                        new RelationShip("女儿", "relationship_13.png"),
//                        new RelationShip("女婿", "relationship_7.png"),
//                        new RelationShip("孙子", "relationship_15.png"),
//                        new RelationShip("孙女", "relationship_16.png"),
//                        new RelationShip("侄子", "relationship_17.png"),
//                        new RelationShip("侄女", "relationship_8.png"),
//                        new RelationShip("家人", "relationship_9.png")};
                        new RelationShip(context.getString(R.string.relation_Son), "relationship_1.png"),
                        new RelationShip(context.getString(R.string.relation_Daughter_in_law), "relationship_2.png"),
                        new RelationShip(context.getString(R.string.relation_Daughter), "relationship_13.png"),
                        new RelationShip(context.getString(R.string.relation_Son_in_law), "relationship_7.png"),
                        new RelationShip(context.getString(R.string.relation_Grandson), "relationship_15.png"),
                        new RelationShip(context.getString(R.string.relation_Granddaughter), "relationship_16.png"),
                        new RelationShip(context.getString(R.string.relation_Nephew), "relationship_17.png"),
                        new RelationShip(context.getString(R.string.relation_Niece), "relationship_8.png"),
                        new RelationShip(context.getString(R.string.relation_family), "relationship_9.png")};
            }else if (controller.isPet_product_enable()){
                res = new RelationShip[]{
                        new RelationShip(context.getString(R.string.pet_relation_1), "relationship_brother.png"),
                        new RelationShip(context.getString(R.string.pet_relation_2), "relationship_sister.png"),
                        new RelationShip(context.getString(R.string.pet_relation_3), "relationship_17.png"),
                        new RelationShip(context.getString(R.string.pet_relation_4), "relationship_13.png"),
                        new RelationShip(context.getString(R.string.pet_relation_5), "relationship_3.png"),
                        new RelationShip(context.getString(R.string.pet_relation_6), "relationship_4.png"),
                        new RelationShip(context.getString(R.string.pet_relation_7), "relationship_1.png"),
                        new RelationShip(context.getString(R.string.pet_relation_8), "relationship_2.png"),
                        new RelationShip(context.getString(R.string.relation_family), "relationship_9.png")};
            }else{
                if(Locale.SIMPLIFIED_CHINESE.equals(context.getResources().getConfiguration().locale)) {//老外不区分外公外婆
                    res = new RelationShip[]{
                        new RelationShip("爸爸", "relationship_1.png"),
                        new RelationShip("妈妈", "relationship_2.png"),
                        new RelationShip("爷爷", "relationship_3.png"),
                        new RelationShip("奶奶", "relationship_4.png"),
                        new RelationShip("外公", "relationship_5.png"),
                        new RelationShip("外婆", "relationship_6.png"),
                        new RelationShip("叔叔", "relationship_7.png"),
                        new RelationShip("阿姨", "relationship_8.png"),
                        new RelationShip("家人", "relationship_9.png")};
                }else{
                    res = new RelationShip[]{
                        new RelationShip(context.getString(R.string.relation_dad), "relationship_1.png"),
                        new RelationShip(context.getString(R.string.relation_mom), "relationship_2.png"),
                        new RelationShip(context.getString(R.string.relation_grandpa), "relationship_3.png"),
                        new RelationShip(context.getString(R.string.relation_grandma), "relationship_4.png"),
                        new RelationShip(context.getString(R.string.relation_brother), "relationship_brother.png"),
                        new RelationShip(context.getString(R.string.relation_sister), "relationship_sister.png"),
                        new RelationShip(context.getString(R.string.relation_uncle), "relationship_7.png"),
                        new RelationShip(context.getString(R.string.relation_aunt), "relationship_8.png"),
                        new RelationShip(context.getString(R.string.relation_family), "relationship_9.png")};
                }
            }
    }

    private RelationShip[] res;


    public RelationShip getSelectedOne(){
        return res[selectedPosition];
    }

    public void setSelectedOne(int newSelectedPosition){
        selectedPosition = newSelectedPosition;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return res.length;
    }

    @Override
    public Object getItem(int i) {
        return res[i];
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = inflater.inflate(R.layout.relation_gridview_item,null);
            holder = new ViewHolder();
            holder.header_iv = (ImageView) convertView.findViewById(R.id.header_iv);
            holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
            holder.root = convertView.findViewById(R.id.root);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        try {
            Bitmap bitmap = BitmapFactory.decodeStream(context.getAssets().open(res[i].picPath));
            holder.header_iv.setImageBitmap(bitmap);
            holder.name_tv.setText(res[i].name);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (i==selectedPosition){
            holder.root.setBackgroundResource(R.drawable.grid_view_item_bg_selected);
        }else{
            holder.root.setBackgroundResource(R.drawable.grid_view_item_bg_normal);
        }

        return convertView;
    }


    static class ViewHolder{
        public ImageView header_iv;
        public TextView name_tv;
        public View root;
    }

}
